package com.open.system.controller;

import com.alibaba.fastjson.JSON;	
import com.open.common.controller.BaseController;
import com.open.common.utils.PageUtils;
import com.open.common.utils.Query;
import com.open.common.utils.R;
//import com.open.loan.utils.sms.SmsTools;
import com.open.system.domain.LoanMsgConfigDO;
import com.open.system.service.LoanMsgConfigService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 短信设置
 *
 * @author aaa
 * @email aaax
 * @date 2018-10-31 22:27:01
 */

@Controller
@RequestMapping("/system/loanMsgConfig")
public class LoanMsgConfigController extends BaseController {
    @Autowired
    private LoanMsgConfigService loanMsgConfigService;
    
    private static NumberFormat nf = new DecimalFormat("000000");

    @GetMapping()
    @RequiresPermissions("system:loanMsgConfig:loanMsgConfig")
    String LoanMsgConfig() {
        return "system/loanMsgConfig/loanMsgConfig";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:loanMsgConfig:loanMsgConfig")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<LoanMsgConfigDO> loanMsgConfigList = loanMsgConfigService.list(query);
        int total = loanMsgConfigService.count(query);
        PageUtils pageUtils = new PageUtils(loanMsgConfigList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("system:loanMsgConfig:add")
    String add() {
        return "system/loanMsgConfig/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("system:loanMsgConfig:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        LoanMsgConfigDO loanMsgConfig = loanMsgConfigService.get(id);
        model.addAttribute("loanMsgConfig", loanMsgConfig);
        return "system/loanMsgConfig/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:loanMsgConfig:add")
    public R save(LoanMsgConfigDO loanMsgConfig) {
        loanMsgConfig.setCreateTime(new Date());
        if (loanMsgConfigService.save(loanMsgConfig) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("system:loanMsgConfig:edit")
    public R update(LoanMsgConfigDO loanMsgConfig) {
        loanMsgConfigService.update(loanMsgConfig);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:loanMsgConfig:remove")
    public R remove(Long id) {
        if (loanMsgConfigService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:loanMsgConfig:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        loanMsgConfigService.batchRemove(ids);
        return R.ok();
    }

    /**
     * 发送短信
     */
    @PostMapping("/sendMsg/{id}")
    @ResponseBody
    @RequiresPermissions("system:loanMsgConfig:loanMsgConfig")
    public R sendMsg(@PathVariable("id") Long id, Model model) {
        LoanMsgConfigDO loanMsgConfigDO = loanMsgConfigService.get(id);
        try {
            if (loanMsgConfigDO != null) {
                String phone = loanMsgConfigDO.getRemark2();
                if("1".equals(loanMsgConfigDO.getRemark3())){//1 既然是成功，如果已经发送就不让在发送了
                	return R.error("此条已发送过了");
                }
                Map<String, Object> params = new HashMap<>();
                //短信
                if("SMS_143660251".equals(loanMsgConfigDO.getContent())){
                	String code = nf.format(new Random().nextInt(999999));
                	params.put("code",code);
                }else if("SMS_152506152".equals(loanMsgConfigDO.getContent())){
                	
                }else if("SMS_152506151".equals(loanMsgConfigDO.getContent())){
                
                }else {
					//其他手机号码
                	return R.error("未找到相应的模板");
				}
                //new SmsTools().sendMessage(phone, JSON.toJSONString(params), loanMsgConfigDO.getContent());
                //更新发送时间
                loanMsgConfigDO.setRemark1(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                loanMsgConfigDO.setRemark3("1");//1：发送成功 2 ：发送失败
                loanMsgConfigService.update(loanMsgConfigDO);
            }
        } catch (Exception e) {
            loanMsgConfigDO.setRemark3("2");//1：发送成功 2 ：发送失败
            loanMsgConfigService.update(loanMsgConfigDO);
            e.printStackTrace();
            return R.error();
        }
        HashMap hashMap = new HashMap();
        hashMap.put("code", "0");
        return R.ok(hashMap);
    }

}
